package com.xuecheng.media.api.controller;


import com.xuecheng.base.model.PageParam;
import com.xuecheng.base.model.PageResult;
import com.xuecheng.media.model.dto.QueryMediaParamsDto;
import com.xuecheng.media.model.dto.UploadFileParamDto;
import com.xuecheng.media.model.dto.UploadFileResultDto;
import com.xuecheng.media.model.po.MediaFiles;
import com.xuecheng.media.service.MediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

/**
 * @description 媒资文件管理接口
 * @author Mr.M
 * @date 2022/9/6 11:29
 * @version 1.0
 */
 @Api(value = "媒资文件管理接口",tags = "媒资文件管理接口")
 @RestController
 @Slf4j(topic = "MediaFilesController")
public class MediaFilesController {


  @Autowired
  MediaFileService mediaFileService;


 @ApiOperation("媒资列表查询接口")
 @PostMapping("/files")
 public PageResult<MediaFiles> list(PageParam pageParams, @RequestBody QueryMediaParamsDto queryMediaParamsDto){

     Long companyId = 1232141425L;

  return mediaFileService.queryMediaFiels(companyId,pageParams,queryMediaParamsDto);

 }

 @ApiOperation("上传图片")
 @PostMapping(value = "/upload/coursefile",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
 public UploadFileResultDto upload(@RequestPart("filedata") MultipartFile filedata) throws IOException {

     //准备上传文件的信息
     UploadFileParamDto uploadFileParamDto = new UploadFileParamDto();

     //原始文件名称
     uploadFileParamDto.setFilename(filedata.getOriginalFilename());

     //文件大小
     uploadFileParamDto.setFileSize(filedata.getSize());

     //文件类型
     uploadFileParamDto.setFileType("001001");


     //接收到文件了

     //机构ID
     Long companyId = 1232141425L;

     //创建一个临时文件
     File tempFile = File.createTempFile("minio", ".temp");

     log.info("tempFile 临时文件:{}",tempFile);

     filedata.transferTo(tempFile);

     //文件路径
     String localFilePath = tempFile.getAbsolutePath();

     log.info("文件路径-localFilePath:{}",localFilePath);

     //调用Service层上传图片
     UploadFileResultDto uploadFileResultDto = mediaFileService.uploadFile(companyId, uploadFileParamDto, localFilePath);

     return uploadFileResultDto;
 }


}
